package com.yunchang.open.iploaction.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * IO工具类
 * Created by jasontujun on 2019/7/23.
 */
public abstract class IOUtil {

    private static final Logger logger = LoggerFactory.getLogger(IOUtil.class);
    private static final int BUFFER_SIZE = 8192;

    /**
     * 将数据流转为（内存中的）字节数组
     * @param inputStream 数据流
     * @return 返回字节数组
     * @throws IOException
     */
    public static byte[] read(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = null;
        try {
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[BUFFER_SIZE];
            int n = 0;
            while (-1 != (n = inputStream.read(buffer))) {
                outputStream.write(buffer, 0, n);
            }
            return outputStream.toByteArray();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }
}
